/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.juick.mod;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * @author ugnich
 */
public class MainActivity extends Activity {
	public static SharedPreferences settings;
	
    public static String nick;
    public static String password;

    public static int theme;
    public static int dividerHeight;
    public static int backgroundColor;
    public static int msgBackgroundColor;
    public static int infoBackgroundColor;
    public static int linkTextColor;
    public static int unameTextColor;
    public static int textColor;
    public static float textSize;
    public static int infoTextColor;
    public static int headerTextColor;
    public static int ulResId;
    public static int urResId;
    public static int dlResId;
    public static int drResId;
    public static int avatarSize;
    public static int textPadding;
    public static Boolean round = true;
    public static Boolean loadAvatar = false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    	settings = PreferenceManager.getDefaultSharedPreferences(this);
    	nick = settings.getString("nick", "");
    	password = settings.getString("password", null);
    
   	    if (password == null) {
            startActivityForResult(new Intent(this, PreferencesActivity.class), 0);
            return;
        }
        Intent intentHome = new Intent(this, MessagesActivity.class);
        intentHome.putExtra("home", true);
        startActivity(intentHome);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            onCreate(Bundle.EMPTY);
        } else {
            finish();
        }
    }

    public static int doHttpGetRequest(String url) {
        try {
            HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
            conn.setUseCaches(false);
            conn.connect();
            int status = conn.getResponseCode();
            conn.disconnect();
            return status;
        } catch (Exception e) {
            Log.e("doHttpGetRequest", e.toString());
        }
        return 0;
    }

    public static boolean hasAuth(Context context) {
        return settings.contains("password");
    }

    public static String getJSON(Context context, String url) {
        String ret = null;
        try {
            URL jsonURL = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) jsonURL.openConnection();

            String authStr = nick + ":" + password;
            if (!authStr.equals(":")) {
                String basicAuth = "Basic " + Base64.encodeToString(authStr.getBytes(), Base64.NO_WRAP);
                conn.setRequestProperty("Authorization", basicAuth);
            }

            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.connect();
            if (conn.getResponseCode() == 200) {
                ret = streamToString(conn.getInputStream());
            } 
            conn.disconnect();
        } catch (Exception e) {
            Log.e("getJSON", e.toString());
        }
        return ret;
    }

    public static String postJSON(Context context, String url, String data) {
        String ret = null;
        try {
            URL jsonURL = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) jsonURL.openConnection();

            String authStr = nick + ":" + password;
            if (!authStr.equals(":")) {
                String basicAuth = "Basic " + Base64.encodeToString(authStr.getBytes(), Base64.NO_WRAP);
                conn.setRequestProperty("Authorization", basicAuth);
            }

            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.connect();

            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.close();

            if (conn.getResponseCode() == 200) {
                ret = streamToString(conn.getInputStream());
            }

            conn.disconnect();
        } catch (Exception e) {
            Log.e("getJSON", e.toString());
        }
        return ret;
    }

    public static String streamToString(InputStream is) {
        try {
            BufferedReader buf = new BufferedReader(new InputStreamReader(is));
            StringBuilder str = new StringBuilder();
            String line;
            do {
                line = buf.readLine();
                str.append(line + "\n");
            } while (line != null);
            return str.toString();
        } catch (Exception e) {
            Log.e("streamReader", e.toString());
        }
        return null;
    }
    
    public static Bitmap downloadImage(String url) {
        try {
            URL imgURL = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imgURL.openConnection();
            conn.setDoInput(true);
            conn.connect();
            return BitmapFactory.decodeStream(conn.getInputStream());
        } catch (Exception e) {
            Log.e("downloadImage", e.toString());
        }
        return null;
    }

}
